/*!
 * \file frlist_show_all.c
 *
 * All Rights Reserved Copyright (C) 2014 broada,Co.,Ltd.\n
 *
 * \version 3.0
 * \author geyh
 * \date 2016/03/31
 *
 */
#include <CUnit/Basic.h>
#include <stdlib.h>

#include "frcomm.h"
#include "frmem.h"
#include "frlist.h"
#include "conntrack.h"
#include "frct_comm.h"

#include "apmtest_comm.h"

static int __suite_init(void)
{

    return 0;
}

static int __suite_cleanup(void)
{

    return 0;
}


static void __my_list_node_free(Frlist_node *node)
{
    frfree(node->data);
}


static void __pf_show(Frlist_node *p_st_node, char **pp_out)
{
    char *p_out = NULL;
    p_out = (char *)frmalloc(strlen((char *)p_st_node->data) + 1);
    snprintf(p_out,128,"%s",(char *)p_st_node->data);
    *pp_out = p_out;
}

static void __case1(void)
{
    Frlist_header *list = NULL;
    Frlist_node *list_node = NULL;
    char *p_data = NULL;
    char *p_out = NULL;

    list = frlist_create();
    p_data = (char *)frmalloc(sizeof(char) + 1);
    snprintf(p_data,2,"%s","6");

    list_node = frlist_node_create(p_data);
    frlist_add_tail(list,list_node);

    p_data = (char *)frmalloc(sizeof(char) + 1);
    snprintf(p_data,2,"%s","1");

    list_node = frlist_node_create(p_data);
    frlist_add_tail(list,list_node);

    p_data = (char *)frmalloc(sizeof(char) + 1);
    snprintf(p_data,2,"%s","2");

    list_node = frlist_node_create(p_data);
    frlist_add_tail(list,list_node);

    p_data = (char *)frmalloc(sizeof(char) + 1);
    snprintf(p_data,2,"%s","3");

    list_node = frlist_node_create(p_data);
    frlist_add_tail(list,list_node);

    p_data = (char *)frmalloc(sizeof(char) + 1);
    snprintf(p_data,2,"%s","4");

    list_node = frlist_node_create(p_data);
    frlist_add_tail(list,list_node);

    p_data = (char *)frmalloc(sizeof(char) + 1);
    snprintf(p_data,2,"%s","5");

    list_node = frlist_node_create(p_data);
    frlist_add_tail(list,list_node);

    frlist_show_all(list,&p_out,&__pf_show);
    CU_ASSERT_STRING_EQUAL(p_out,"612345");

    frfree(p_out);
    frlist_destroy(list,&__my_list_node_free);
    return;
}

static void __case2(void)
{
    Frlist_header *list = NULL;
    Frlist_node *list_node = NULL;
    char *p_data = NULL;
    char *p_out = NULL;

    list = frlist_create();
    p_data = (char *)frmalloc(sizeof(char) + 1);
    snprintf(p_data,2,"%s","6");

    list_node = frlist_node_create(p_data);
    frlist_add_tail(list,list_node);

    p_data = (char *)frmalloc(sizeof(char) + 1);
    snprintf(p_data,2,"%s","1");

    list_node = frlist_node_create(p_data);
    frlist_add_tail(list,list_node);

    p_data = (char *)frmalloc(sizeof(char) + 1);
    snprintf(p_data,2,"%s","2");

    list_node = frlist_node_create(p_data);
    frlist_add_tail(list,list_node);

    p_data = (char *)frmalloc(sizeof(char) + 1);
    snprintf(p_data,2,"%s","3");

    list_node = frlist_node_create(p_data);
    frlist_add_tail(list,list_node);

    p_data = (char *)frmalloc(sizeof(char) + 1);
    snprintf(p_data,2,"%s","4");

    list_node = frlist_node_create(p_data);
    frlist_add_tail(list,list_node);

    p_data = (char *)frmalloc(sizeof(char) + 1);
    snprintf(p_data,2,"%s","5");

    list_node = frlist_node_create(p_data);
    frlist_add_tail(list,list_node);

    frlist_show_all(list,&p_out,NULL);
    CU_ASSERT_PTR_NULL(p_out);

    frlist_destroy(list,&__my_list_node_free);
    return;
}

int add_suite_frlist_show_all(void)
{
    CU_pSuite pSuite = NULL;

    if ((pSuite = CU_add_suite("frlist frlist_show_all()", __suite_init, __suite_cleanup)) == NULL)
        return FR_FAIL;

    CU_add_test(pSuite, "case1", __case1);
    CU_add_test(pSuite, "case2", __case2);

    return FR_SUCC;
}
